import 'package:flutter/material.dart';
import 'person.dart';
import 'animal.dart';
import 'rect.dart';
import 'good.dart';
import 'programing_language.dart';
import 'traffic.dart';

class ClassesPage extends StatefulWidget {
  final Map<String, dynamic> arguments;
  ClassesPage({Key key, this.arguments}) : super(key: key);

  @override
  _ClassesPageState createState() => _ClassesPageState();
}

class _ClassesPageState extends State<ClassesPage> {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Scaffold(
        appBar: PreferredSize(
          child: AppBar(
            title: Text('ClassesPage'),
          ),
          preferredSize: Size.fromHeight(44.0),
        ),
        body: ListView(
          children: <Widget>[
            ListTile(
              title: Text('默认构造函数'),
              onTap: () {
                final p = Person();
                // final p = Person('Coder', 18);
                p.eat('apple');
              },
            ),
            Divider(height: 1.0),
            ListTile(
              title: Text('命名构造函数'),
              onTap: () {
                final now = DateTime.now();
                print(now);

                final p = Person.create(name: 'Westlife', age: 28);
                p.eat('banana');
              },
            ),
            Divider(height: 1.0),
            ListTile(
              title: Text('私有 _'),
              onTap: () {
                final a = Animal('loulou', '8');
                a.run();
                print(a.name);
                print(a.age);
                // print(a._feet); // 无法访问
                print(a.getFeet());
              },
            ),
            Divider(height: 1.0),
            ListTile(
              title: Text('get set'),
              onTap: () {
                final r = Rect(10, 20);
                print(r.area);
                r.square = 30;
                print(r.area);
              },
            ),
            Divider(height: 1.0),
            ListTile(
              title: Text('static'),
              onTap: () {
                print(Good.goodId);
                Good.buy();
              },
            ),
            Divider(height: 1.0),
            ListTile(
              title: Text('对象操作符 ? as is ..'),
              onTap: () {
                // ?
                final p = Person.create(name: 'Jone', age: 18);
                p?.eat('apple');

                // is (是否为某一类)
                if (p is Person) {
                  print('p is Person');
                }

                final ania = Ania();
                if (ania is Person) {
                  print('ania is Person');
                }

                // as (类型转换)
                final Map<String, dynamic> map = {
                  'a': Ania(),
                };
                final value = map['a'];
                final a = value as Ania;
                a.studing();

                // .. (级联操作)
                final r = Rect(20, 20);
                r
                  ..width = 30
                  ..height = 40
                  ..countArea();
              },
            ),
            Divider(height: 1.0),
            ListTile(
              title: Text('extends (继承 <重写>)'),
              subtitle: Text('''
- 子类继承父类可见的属性与方法 不继承构造函数;
- 子类可复写父类 getter setter;'''),
              onTap: () {
                final p1 = ProgramingLanguage('p1', 100);
                // final p2 = ProgramingLanguage.namedFunc('p2', 200);
                final s1 = Swift('s1', 7, 'iOS');
                // final s2 = Swift.namedFunc('s2', 8, 'iMac');

                p1.developforComputer();
                s1.developforComputer();
                s1.developforiOS();
              },
            ),
            Divider(height: 1.0),
            ListTile(
              title: Text('abstract & implements (抽象 & 接口 & 多接口)'),
              subtitle: Text('''
- 抽象类无法直接实例化;
- 抽象类定义标准和约束;
- 实现接口时必须实现所有属性和方法;'''),
              onTap: () {
                // abstract
                // final t = Traffic(); // Abstract classes can't be instantiated.
                final c = Car();
                c.startUp();
                c.brake();
                c.repair();
                Traffic.countWheels();

                // implements
                final b = Bus('BYD');
                print(b.name);
                b.startUp();
                b.brake();
                b.repair();
              },
            ),
            Divider(height: 1.0),
            ListTile(
              title: Text('with (mixins)'),
              subtitle: Text('''
- 被 mixins 的类必须直接继承自 Object;
- mixins 非继承 非接口;
- mixins 类不能有构造函数;
- 一个类可以 mixins 多个 mixins 类;
- 多 mixins 时, 方法后面覆盖前面;
- extends 和 mixins 时, 方法后面覆盖前面;
- mixins 类型为超类 Object 的子类型'''),
              onTap: () {
                final d = Dog();
                d.wangwang();

                final r = Robot();
                r.wangwang();
                r.miaomiao();

                // mixins 类型为超类 Object 的子类型
                final a = ArtificialIntelligence(); // true
                print(a is Dog); // true
                print(a is Cat); // true
                print(a is Traffic); // true
                print(a is AssembiedInChina); // true
              },
            ),
            Divider(height: 1.0),
            ListTile(
              title: Text('写法先后顺序: extends -> with -> implements'),
              onTap: () {},
            ),
            Divider(height: 1.0),
            ListTile(
              title: Text('多态'),
              onTap: () {
                final airplane = B2();
                airplane.startUp();
              },
            ),
            Divider(height: 1.0),
          ],
        ),
      ),
    );
  }
}
